package cn.seecoder.courselearning.mapperservice.course;

import cn.seecoder.courselearning.po.course.RentUserDO;
import cn.seecoder.courselearning.vo.course.RentCourseVO;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

/**
 * @author wangzb
 * @date 2021/7/8
 * @description
 */
@Mapper
public interface RentCourseMapper {

    @Results(id = "info",value = {
            @Result(column = "id",property = "id"),
            @Result(column = "NAME",property = "name"),
            @Result(column = "type",property = "type"),
            @Result(column = "intro",property = "intro"),
            @Result(column = "picture",property = "picture"),
            @Result(column = "school",property = "school"),
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "cost",property = "cost"),
            @Result(column = "teacher_id",property = "teacherId"),
            @Result(column = "teacher_name",property = "teacherName"),
            @Result(column = "likes",property = "likes"),
            @Result(column = "start_time",property = "startTime"),
            @Result(column = "end_time",property = "endTime"),
            @Result(column = "rent_id",property = "rentId"),
            @Result(column = "is_rent",property = "isRent")
    })
    @Select("SELECT  " +
            "  c.id,  " +
            "  NAME,  " +
            "  type,  " +
            "  intro,  " +
            "  picture,  " +
            "  school,  " +
            "  c.create_time,  " +
            "  delete_time,  " +
            "  cost,  " +
            "  teacher_id,  " +
            "  teacher_name,  " +
            "  a.start_time,  " +
            "  a.end_time," +
            "  a.rent_id," +
            "  IF(a.rent_id,1,0) as is_rent  " +
            "FROM  " +
            "  course c ," +
            "  rent_course a   " +
            "WHERE  " +
            "  a.cource_id = c.id   " +
            "  AND a.enabled= 1  " +
            "  AND a.user_id = #{userId}  " +
            "  ORDER BY a.create_time DESC")
    List<RentCourseVO> qryRentList(Integer userId);

    @Select("select a.rent_id from rent_course a where a.user_id=#{userId} and a.cource_id=#{courseId} and #{date} BETWEEN a.start_time AND a.end_time and enabled=1 ")
    Integer judge(Integer userId, Integer courseId, Date date);

    @Update("UPDATE `rent_course` SET `enabled` = 0 WHERE `rent_id` = #{judge}")
    void remove(Integer judge);

    @Update("UPDATE `user_info` SET `balance` =balance-#{money}  WHERE `id` = #{uid}")
    void updateMoney(Integer uid,Long money);

    @Results({
        @Result(column = "rent_id",property = "rentId"),
        @Result(column = "is_rent",property = "isRent"),
    })
    @Select("SELECT  " +
            "c.id,  " +
            "a.rent_id,  " +
            "c.balance,  " +
            "IF  " +
            "  ( a.rent_id, 1, 0 ) AS is_rent   " +
            "FROM  " +
            "  user_info c  " +
            "  LEFT JOIN rent_course a ON a.user_id = c.id   " +
            "  AND a.enabled = 1   " +
            "  AND a.user_id = #{userId}  " +
            "  AND a.cource_id=#{courseId} " +
            "WHERE  " +
            "  c.id = #{userId}")
    RentUserDO info(Integer courseId, Integer userId);

    @Insert("INSERT INTO `rent_course`(`user_id`, `cource_id`, `start_time`, `end_time`, `create_time`) VALUES (#{userId}, #{courseId}, #{startTime}, #{endTime},  #{startTime})")
    void addRent(Integer courseId, Integer userId, Date startTime, Date endTime);
}
